package com.han.listeners;

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpServletRequest;

/**
 * Application Lifecycle Listener implementation class TestListener
 *
 */
@WebListener
public class JMesaExportStudentExamListListener implements ServletRequestListener {

    /**
     * Default constructor. 
     */
    public JMesaExportStudentExamListListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletRequestListener#requestDestroyed(ServletRequestEvent)
     */
    public void requestDestroyed(ServletRequestEvent arg0) {
        // TODO Auto-generated method stub
    	String URI = ((HttpServletRequest)arg0.getServletRequest()).getRequestURI();
    	
    	if(!(".css".equals(URI.substring(URI.length()-4)) || ".js".equals(URI.substring(URI.length()-3)))){
    		if(!("/BetaTestEngine/Professor/JMesaExportStudentExamListServlet".equals(URI))){
    			if(!"/BetaTestEngine/LogoutServlet".equals(URI)){
    				((HttpServletRequest)arg0.getServletRequest()).getSession().removeAttribute("examID");
    				//System.out.println("DESTROYING EXAMRESPONSE SESSION ATTRIB");
    			}
    		}
    	}
    }

	/**
     * @see ServletRequestListener#requestInitialized(ServletRequestEvent)
     */
    public void requestInitialized(ServletRequestEvent arg0) {
        // TODO Auto-generated method stub
    	String URI = ((HttpServletRequest)arg0.getServletRequest()).getRequestURI();
    	if("/BetaTestEngine/Professor/JMesaExportStudentExamListServlet".equals(URI)){
    		
    		if(((HttpServletRequest)arg0.getServletRequest()).getParameter("id")!=null){
	    
	    		String examResponseID = ((HttpServletRequest)arg0.getServletRequest()).getParameter("id");
	    		
	    		((HttpServletRequest)arg0.getServletRequest()).getSession().setAttribute("examID", examResponseID);
	    		
				//System.out.println("CREATING EXAMRESPONSE SESSION ATTRIB: "+examResponseID);
				
    		}
    	}
    }
	
}
